package fr.mistra.exercice.bibliotheque.presentation;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import fr.mistra.exercice.bibliotheque.dao.DAOException;
import fr.mistra.exercice.bibliotheque.modeles.Bibliotheque;
import fr.mistra.exercice.bibliotheque.services.BibliothequeService;

/**
 * Servlet implementation class VoirBibliotheques
 */
public class VoirBibliothequesServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public VoirBibliothequesServlet() {
        super();
        System.out.println("Lancement du constructeur de la servlet VoirBibliotheques");
    }

    public void finalize() throws Throwable {
        System.out.println("Destruction de la servlet VoirBibliotheques");
    	super.finalize();
    }
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			BibliothequeService service = BibliothequeService.getService();
			List<Bibliotheque> listeBiblio = service.getBibliotheques();
			request.setAttribute("listeBiblio", listeBiblio);
			System.out.println("Go liste biblio !!!");
			request.getRequestDispatcher("/JSP/VoirBibliotheques.jsp").forward(request, response);
		} catch (DAOException e) {
			e.printStackTrace();
			request.getRequestDispatcher("/JSP/ErreurDAO.jsp").forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//Aucune raison d'arriver l� via une m�thode POST
		request.getRequestDispatcher("/curieux.jsp").forward(request, response);
	}
}
